import nattyFetch from 'natty-fetch';
import { urlPrefix, isDev } from './variables';
const xmlToJSON = require('xmlToJSON');

// See https://github.com/Jias/natty-fetch for more details.
const context = nattyFetch.context({
  urlStamp: false,
  urlMark: false,
  timeout: 5000,
  header:{
    accept: 'application/xml'
  },
  fit(response) {
    if (response) {
      try {
        response = JSON.parse(response);
        return {
          success: response,
          content: response,
          error: {
            errorMsg: response.errorMsg,
            errorCode: response.errorCode,
            errorLevel: response.errorLevel,
          },
        };
      }
      catch(e){
        response = xmlToJSON.parseString(response)
        return {
          success: response,
          content: response,
          error: {
            errorMsg: response.errorMsg,
            errorCode: response.errorCode,
            errorLevel: response.errorLevel,
          },
        };
      }
    }else {
      return {
        success: true,
      }
    }
  },
});

context.create({
  // 这里可以通过`abc.json`设置模拟数据
  'Bus.line': {
    url: '/mapAPI/getRouteNamesByKeyword',
    method: 'GET'
  },
  'Bus.routes': {
    url: '/mapAPI/getRouteByRouteName',
    method: 'GET'
  },
});

export default context.api;
